﻿using Microsoft.Xna.Framework;
using System;
using Terraria;
using Terraria.GameContent.Bestiary;
using Terraria.ID;
using Terraria.ModLoader;

namespace HardcoreGame.Content.NPCs.Enemies.Events.FrostLegion
{
    public class RPGSnowman : ModNPC
    {
        public override void SetStaticDefaults()
        {
            Main.npcFrameCount[NPC.type] = 4;
        }
        public override void SetDefaults()
        {
            NPC.lifeMax = 170;
            NPC.damage = 30;
            NPC.defense = 15;
            NPC.knockBackResist = 0.2f;
            NPC.width = 42;
            NPC.aiStyle = 41;
            NPC.height = 56;
            NPC.aiStyle = 38;
            NPC.npcSlots = 15f;
            NPC.HitSound = SoundID.NPCHit11;
            NPC.DeathSound = SoundID.NPCDeath15;
            NPC.value = Item.buyPrice(0, 0, 1, 21);
            AnimationType = 143;
            Music = MusicID.Boss3;
        }
        public override float SpawnChance(NPCSpawnInfo spawnInfo)
        {
            if (!NPC.AnyNPCs(144) || !Main.hardMode || spawnInfo.SpawnTileY >= Main.worldSurface)
            {
                return 0f;
            }
            return 0.06f;
        }
        public override void AI()
        {
            NPC.ai[2] += 1f;
            if (NPC.ai[2] >= 80f)
            {
                NPC.ai[2] = 0f;
                if (Main.netMode != NetmodeID.MultiplayerClient)
                {
                    Vector2 vector70 = new(NPC.position.X + NPC.width * 0.5f - NPC.direction * 12, NPC.position.Y + NPC.height * 0.5f);
                    int[] projectileTypes = [134, 140, 776];
                    int selectedType = projectileTypes[Main.rand.Next(projectileTypes.Length)];
                    float baseSpeed = 10f;
                    float angleOffset = MathHelper.ToRadians(Main.rand.Next(-10, 11));
                    float speedX = baseSpeed * NPC.spriteDirection * (float)Math.Cos(angleOffset);
                    float speedY = baseSpeed * (float)Math.Sin(angleOffset);
                    int damage = 25;
                    int numProjectile = Projectile.NewProjectile(NPC.GetSource_FromAI(), vector70.X, vector70.Y, speedX, speedY, selectedType, damage, 0f, Main.myPlayer);
                    Main.projectile[numProjectile].ai[0] = 2f;
                    Main.projectile[numProjectile].timeLeft = 300;
                    Main.projectile[numProjectile].friendly = false;
                    NetMessage.SendData(MessageID.SyncProjectile, -1, -1, null, numProjectile);
                    NPC.netUpdate = true;
                    ApplyRecoil();
                }
            }
        }
        private void ApplyRecoil()
        {
            float recoilStrength = 2.5f;
            NPC.velocity.X -= NPC.spriteDirection * recoilStrength;
            NPC.velocity.Y += (Main.rand.NextFloat() - 0.5f) * 1.5f;
            NPC.immune[Main.myPlayer] = 10;
        }
        public override void ModifyNPCLoot(NPCLoot npcLoot)
        {
            var DropRules = Main.ItemDropsDB.GetRulesForNPCID(NPCID.SnowBalla, false);
            foreach (var DropRule in DropRules)
            {
                npcLoot.Add(DropRule);
            }
        }
        public override void SetBestiary(BestiaryDatabase database, BestiaryEntry bestiaryEntry)
        {
            bestiaryEntry.Info.AddRange([
                BestiaryDatabaseNPCsPopulator.CommonTags.SpawnConditions.Invasions.FrostLegion,
                new FlavorTextBestiaryInfoElement("Mods.HardcoreGame.NPCs.RPGSnowman.Bestiary")
            ]);
        }
        public override void HitEffect(NPC.HitInfo hit)
        {
            if (NPC.life > 0)
            {
                for (int num395 = 0; num395 < hit.Damage / (double)NPC.lifeMax * 100.0; num395++)
                {
                    int num396 = Dust.NewDust(NPC.position, NPC.width, NPC.height, DustID.Snow, hit.HitDirection, -1f);
                    Main.dust[num396].noGravity = true;
                }
            }
            else
            {
                for (int num397 = 0; num397 < 50; num397++)
                {
                    int num398 = Dust.NewDust(NPC.position, NPC.width, NPC.height, DustID.Snow, hit.HitDirection, -1f);
                    Main.dust[num398].noGravity = true;
                    Dust dust = Main.dust[num398];
                    dust.scale *= 1.2f;
                }
            }
        }
    }
}
